<html>
<head><meta charset="utf-8"><title>Why does clippy use a custom hasher? · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html">Why does clippy use a custom hasher?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217567644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217567644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217567644">(Nov 22 2020 at 20:11)</a>:</h4>
<p>For <a href="https://github.com/rust-lang/rust/pull/78218">https://github.com/rust-lang/rust/pull/78218</a> I'm switching <code>FxHash</code> to <code>AHash</code>. That gave an error in clippy:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">error</span><span class="p">[</span><span class="n">E0107</span><span class="p">]</span>: <span class="nc">wrong</span><span class="w"> </span><span class="n">number</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="k">type</span> <span class="nc">arguments</span>: <span class="nc">expected</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">found</span><span class="w"> </span><span class="mi">3</span><span class="w"></span>
<span class="w">    </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">tools</span><span class="o">/</span><span class="n">clippy</span><span class="o">/</span><span class="n">clippy_lints</span><span class="o">/</span><span class="n">src</span><span class="o">/</span><span class="n">utils</span><span class="o">/</span><span class="k">mod</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">1503</span>:<span class="mi">40</span><span class="w"></span>
<span class="w">     </span><span class="o">|</span><span class="w"></span>
<span class="mi">1503</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">map</span>: <span class="nc">FxHashMap</span><span class="o">&lt;</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;&amp;</span><span class="n">_</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">BuildHasherDefault</span><span class="o">&lt;</span><span class="n">FxHasher</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="w">     </span><span class="o">|</span><span class="w">                                        </span><span class="o">^^^^^^^^^^^^^^^^^^^^^^^^^^^^</span><span class="w"> </span><span class="n">unexpected</span><span class="w"> </span><span class="k">type</span> <span class="nc">argument</span><span class="w"></span>

<span class="n">error</span><span class="p">[</span><span class="n">E0308</span><span class="p">]</span>: <span class="nc">mismatched</span><span class="w"> </span><span class="n">types</span><span class="w"></span>
<span class="w">    </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">tools</span><span class="o">/</span><span class="n">clippy</span><span class="o">/</span><span class="n">clippy_lints</span><span class="o">/</span><span class="n">src</span><span class="o">/</span><span class="n">utils</span><span class="o">/</span><span class="k">mod</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">1504</span>:<span class="mi">58</span><span class="w"></span>
<span class="w">     </span><span class="o">|</span><span class="w"></span>
<span class="mi">1504</span><span class="w"> </span><span class="o">|</span><span class="w">         </span><span class="n">FxHashMap</span>::<span class="n">with_capacity_and_hasher</span><span class="p">(</span><span class="n">exprs</span><span class="p">.</span><span class="n">len</span><span class="p">(),</span><span class="w"> </span><span class="n">BuildHasherDefault</span>::<span class="n">default</span><span class="p">());</span><span class="w"></span>
<span class="w">     </span><span class="o">|</span><span class="w">                                                          </span><span class="o">^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</span><span class="w"> </span><span class="n">expected</span><span class="w"> </span><span class="k">struct</span> <span class="err">`</span><span class="n">ahash</span>::<span class="n">random_state</span>::<span class="n">RandomState</span><span class="err">`</span><span class="p">,</span><span class="w"> </span><span class="n">found</span><span class="w"> </span><span class="k">struct</span> <span class="err">`</span><span class="n">BuildHasherDefault</span><span class="err">`</span><span class="w"></span>
<span class="w">     </span><span class="o">|</span><span class="w"></span>
<span class="w">     </span><span class="o">=</span><span class="w"> </span><span class="n">note</span>: <span class="nc">expected</span><span class="w"> </span><span class="k">struct</span> <span class="err">`</span><span class="n">ahash</span>::<span class="n">random_state</span>::<span class="n">RandomState</span><span class="err">`</span><span class="w"></span>
<span class="w">                </span><span class="n">found</span><span class="w"> </span><span class="k">struct</span> <span class="err">`</span><span class="n">BuildHasherDefault</span><span class="o">&lt;</span><span class="n">_</span><span class="o">&gt;</span><span class="err">`</span><span class="w"></span>
</code></pre></div>
<p>I can fix it by using <code>std::collections::HashMap</code> directly, but I'm curious - why does clippy go through BuildHasherDefault here? And why does it still use <code>FxHashMap</code> when it does?</p>



<a name="217568107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568107">(Nov 22 2020 at 20:22)</a>:</h4>
<p>I have no idea. Maybe we did something fancy with it before switching to <code>FxHash*</code> and in the move, we just  kept specifying the hasher? Just remove the specific hasher, if it still passes tests, I'm fine with it. <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span>  (You can't use <code>std::HashMap</code> though)</p>



<a name="217568121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568121">(Nov 22 2020 at 20:23)</a>:</h4>
<p>what's wrong with std::HashMap?</p>



<a name="217568199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568199">(Nov 22 2020 at 20:24)</a>:</h4>
<p>Internal rustc lint says, that we want to only use <code>FxHashMap</code>in the compiler and by extension in Clippy. That's because, we don't need secure hashing algorithms in compilers and can use faster algorithms, or something like this.</p>



<a name="217568218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568218">(Nov 22 2020 at 20:25)</a>:</h4>
<p>well, they're the same type</p>



<a name="217568226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568226">(Nov 22 2020 at 20:26)</a>:</h4>
<p><a href="https://docs.rs/rustc-hash/1.1.0/rustc_hash/type.FxHashMap.html">https://docs.rs/rustc-hash/1.1.0/rustc_hash/type.FxHashMap.html</a></p>



<a name="217568269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568269">(Nov 22 2020 at 20:26)</a>:</h4>
<p>if you specify the hasher, it's exactly the same as using the standard library</p>



<a name="217568340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568340">(Nov 22 2020 at 20:28)</a>:</h4>
<p>Yeah, then I guess the solution is to just not specify the hasher and hope, that it doesn't break something?</p>



<a name="217568353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568353">(Nov 22 2020 at 20:29)</a>:</h4>
<p>for now I used</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">collections</span>::<span class="n">HashMap</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">map</span>: <span class="nc">HashMap</span><span class="o">&lt;</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;&amp;</span><span class="n">_</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">BuildHasherDefault</span><span class="o">&lt;</span><span class="n">FxHasher</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="w">        </span><span class="n">HashMap</span>::<span class="n">with_capacity_and_hasher</span><span class="p">(</span><span class="n">exprs</span><span class="p">.</span><span class="n">len</span><span class="p">(),</span><span class="w"> </span><span class="n">BuildHasherDefault</span>::<span class="n">default</span><span class="p">());</span><span class="w"></span>
</code></pre></div>
<p>if it ends up being a perf improvement I'll change it just to remove the explicit hasher and use <code>FxHashMap</code></p>



<a name="217568429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568429">(Nov 22 2020 at 20:30)</a>:</h4>
<p>You may have to allow <code>rustc::default_hash_types</code> on that function, so that it passes CI.</p>



<a name="217568435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568435">(Nov 22 2020 at 20:30)</a>:</h4>
<p>those are just lints, right? I don't think those run for a try build</p>



<a name="217568447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568447">(Nov 22 2020 at 20:31)</a>:</h4>
<p>the default checks are already failing because ahash isn't whitelisted <a href="https://github.com/rust-lang/rust/runs/1439161965?check_suite_focus=true#step:24:1937">https://github.com/rust-lang/rust/runs/1439161965?check_suite_focus=true#step:24:1937</a></p>



<a name="217568502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Why%20does%20clippy%20use%20a%20custom%20hasher%3F/near/217568502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Why.20does.20clippy.20use.20a.20custom.20hasher.3F.html#217568502">(Nov 22 2020 at 20:32)</a>:</h4>
<p>Clippy has <code>#![deny(rustc::internal)]</code>in its <code>lib.rs</code>. So it should fail CI.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>